# Makefile - 项目管理
.PHONY: help install setup run test clean build deploy backup

# 默认目标
help:
	@echo "可用命令:"
	@echo "  install    - 安装依赖包"
	@echo "  setup      - 初始化项目"
	@echo "  run        - 启动应用"
	@echo "  test       - 运行测试"
	@echo "  clean      - 清理临时文件"
	@echo "  build      - 构建Docker镜像"
	@echo "  deploy     - 部署应用"
	@echo "  backup     - 创建数据备份"

# 安装依赖
install:
	pip install -r requirements.txt

# 初始化项目
setup:
	python scripts/setup.py
	@echo "✅ 项目初始化完成"

# 启动应用
run:
	streamlit run main.py

# 开发模式启动
dev:
	streamlit run main.py --server.runOnSave=true --server.fileWatcherType=watchdog

# 运行测试
test:
	pytest tests/ -v --tb=short

# 测试覆盖率
test-coverage:
	pytest tests/ --cov=. --cov-report=html --cov-report=term

# 代码格式化
format:
	black *.py pages/*.py utils/*.py
	isort *.py pages/*.py utils/*.py

# 代码检查
lint:
	flake8 *.py pages/*.py utils/*.py
	mypy *.py pages/*.py utils/*.py

# 清理临时文件
clean:
	find . -type f -name "*.pyc" -delete
	find . -type d -name "__pycache__" -delete
	rm -rf .pytest_cache/
	rm -rf htmlcov/
	rm -rf .coverage
	@echo "✅ 临时文件已清理"

# 构建Docker镜像
build:
	docker build -f docker/Dockerfile -t streamlit-app .

# 使用Docker Compose启动
up:
	docker-compose -f docker/docker-compose.yml up -d

# 停止Docker服务
down:
	docker-compose -f docker/docker-compose.yml down

# 创建备份
backup:
	python scripts/backup.py create --compress

# 生成用户配置
users:
	python scripts/generate_users.py

# 查看日志
logs:
	tail -f logs/user_activity.json

# 部署到生产环境
deploy:
	@echo "🚀 部署到生产环境..."
	docker-compose -f docker/docker-compose.yml pull
	docker-compose -f docker/docker-compose.yml up -d --remove-orphans
	@echo "✅ 部署完成"

# 更新依赖
update:
	pip list --outdated
	pip-review --auto

# 安全检查
security:
	safety check
	bandit -r . -ll

# 性能测试
benchmark:
	@echo "🔍 运行性能测试..."
	python -m pytest tests/test_performance.py -v